package com.bjsc.controller.movie;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bjsc.common.bean.Rest;
import com.bjsc.common.controller.SuperController;
import com.bjsc.entity.Movie;
import com.bjsc.service.IMovieService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author create by VIC.Huang
 * @date 2018/4/10
 */
@Controller
@RequestMapping("/movie")
public class MovieController extends SuperController {

    @Autowired
    private IMovieService movieService;

    @RequestMapping("/list/{pageNumber}")
    public String list(@PathVariable Integer pageNumber, String search,
                       @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        Page<Movie> page = getPage(pageNumber, pageSize);
        page.setOrderByField("id");
        page.setAsc(false);
        model.addAttribute("pageSize", pageSize);

        // 查询分页
        EntityWrapper<Movie> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            ew.like("title", search);
            model.addAttribute("search", search);
        }

        Page<Movie> pageData = movieService.selectPage(page, ew);
        model.addAttribute("pageData", pageData);
        return "movie/list";
    }

    @RequestMapping("/uploadIndex")
    public String uploadIndex() {
        return "movie/add";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Rest upload(@RequestParam("title") String title, @RequestParam("headImg") String headImg,
                       @RequestParam("movie") String movie) {
        Movie movieEntity = new Movie();
        movieEntity.setTitle(title).setHeadImgPath(headImg).setFilePath(movie);
        if (movieService.insert(movieEntity)) {
            return Rest.ok("上传成功");
        }
        return Rest.failure("上传失败");
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Rest delete(@PathVariable("id")Long id) {
        if(movieService.deleteById(id)) {
            return Rest.ok("删除成功");
        }
        return Rest.failure("删除失败");
    }
}
